package com.skynet.supervision.utils;

import fr.opensagres.poi.xwpf.converter.core.BasicURIResolver;
import fr.opensagres.poi.xwpf.converter.core.FileImageExtractor;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.PictureType;
//import org.apache.poi.xwpf.converter.core.BasicURIResolver;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;


@Component
public class WordToHtmlUtil {



    private static final Logger logger = LoggerFactory.getLogger(WordToHtmlUtil.class);

    /**
     * 上传Word文档，返回解析后的Html
     */
    public static String docToHtmlText(MultipartFile file) throws Exception {
        //使用字符数组流获取解析的内容
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream outStream = new BufferedOutputStream(baos);
        try {
            //将上传的文件传入Document转换
            HWPFDocument wordDocument = new HWPFDocument(file.getInputStream());
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(document);
            //将读取到的图片上传并添加链接地址
            wordToHtmlConverter.setPicturesManager((imageStream, pictureType, name, width, height) -> {
                try {
                    //首先要判断图片是否能识别
                    if (pictureType.equals(PictureType.UNKNOWN)) {
                        return "[不能识别的图片]";
                    }
                    //此处上传到自己的文件服务器 todo
                    String qiNiuName = "";//文件名
//                    boolean upload = FileUtil.upload(new        FileInputStream(fileImage), qiNiuName);
                    return "上传后的图片地址";

                } catch (Exception e) {
                    logger.info("upload exception", e);
                }
                return "[图片上传失败]";
            });
            // word文档转Html文档
            wordToHtmlConverter.processDocument(wordDocument);
            Document htmlDocument = wordToHtmlConverter.getDocument();
            DOMSource domSource = new DOMSource(htmlDocument);
            StreamResult streamResult = new StreamResult(outStream);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer serializer = factory.newTransformer();
            serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty(OutputKeys.METHOD, "html");
            serializer.transform(domSource, streamResult);
            String content = baos.toString();
            logger.info("docToHtmlText--->{}", content);
            return content;
        } catch (Exception e) {
            logger.error("docToHtmlText 异常", e);
//            throw new AppRuntimeException(e);
        } finally {
            baos.close();
            outStream.close();
        }
        return "";
    }

    /**
     * 上传docx文档，返回解析后的Html
     */
    public static String docxToHtmlText(MultipartFile file) throws Exception {
        ByteArrayOutputStream htmlStream = new ByteArrayOutputStream();
        try {
            // 将上传的文件传入Document转换
            XWPFDocument docxDocument = new XWPFDocument(file.getInputStream());
            XHTMLOptions options = XHTMLOptions.create();
            // 设置图片存储路径
//            String path = System.getProperty("java.io.tmpdir");
//            String firstImagePathStr = path + "/" + System.currentTimeMillis();
            String firstImagePathStr = "D://";
            options.setExtractor(new FileImageExtractor(new File(firstImagePathStr)));
            options.URIResolver(new BasicURIResolver(firstImagePathStr));
            // 转换html
            docxDocument.createNumbering();
            XHTMLConverter.getInstance().convert(docxDocument, htmlStream, options);
            String htmlStr = htmlStream.toString();

//            String middleImageDirStr = "/word/media";
//            String imageDirStr = firstImagePathStr + middleImageDirStr;
//            File imageDir = new File(imageDirStr);
//            String[] imageList = imageDir.list();
//            if (imageList != null) {
//                for (int i = 0; i < imageList.length; i++) {
//                    try {
//                        String oneImagePathStr = imageDirStr + "/" + imageList[i];
//                        File fileImage = new File(oneImagePathStr);
//                        if (fileImage.exists()) {
//                            String name = fileImage.getName();
//                            String suffix = name.substring(name.indexOf("."), name.length()).toLowerCase();
//                            //此处上传到自己的文件服务器 todo
//                            String qiNiuName = "";//文件名
////                            boolean upload = FileUtil.upload(new FileInputStream(fileImage), qiNiuName);
//                            String uploadlocal = FilesUtils.uploadlocal((MultipartFile) fileImage);
//                            if (uploadlocal.equals("")) {
//                                continue;
//                            } else {
//                                //修改文档中的图片信息
//                                htmlStr = htmlStr.replace(oneImagePathStr, uploadlocal);
//                            }
//                        }
//                    } catch (Exception e) {
//                        logger.info("upload docxToHtmlText exception", e);
//                    }
//                }
//            }
            //删除图片路径
//            File firstImagePath = new File(firstImagePathStr);
//            FileUtils.deleteDirectory(firstImagePath);
            return htmlStr;
        } catch (Exception e) {
            logger.error("docxToHtmlText 解析异常", e);
//            throw new AppRuntimeException(e);
        } finally {
            if (htmlStream != null) {
                htmlStream.close();
            }
        }
        return "";
    }


   public static void main(String[] args) {
        try {
            File file = new File("D://workcode/通知书/讯问通知书.docx");
            FileInputStream inputStream = new FileInputStream(file);
            MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                    ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
            String content = docxToHtmlText(multipartFile);
            System.out.println(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}
